import tkinter as tk
from tkinter import font

class LeadNameObj:
    TextWidth = 150
    TextSize = 20
    TextType = 'Helvetica'
    Offset = 30
    BackgroundColor = 'yellow'
    IsBold = False

    def get_text_bbox(self, text_id):
        return self.canvas.bbox(text_id)
    def __init__(self, canvas, edge_pos, location,text = '',colorEnable = False, Fontcolor="black", Backgroundcolor=""):
        self.canvas = canvas
        self.edge_pos = edge_pos.upper()
        self.location = location
        self.Fontcolor = Fontcolor if colorEnable else 'black'
        self.background_color = Backgroundcolor if colorEnable else ''
        self.text = text


#    def show(self):
        x, y = self.location

        # Create a font with the specified height and weight
        text_font = font.Font(family=LeadNameObj.TextType, size=LeadNameObj.TextSize, weight='bold' if LeadNameObj.IsBold else 'normal')

        # Create the text (temporarily at location)
        text_id = self.canvas.create_text(
            int(x), int(y),
            text=self.text,
            font=text_font,
            fill=self.Fontcolor
        )

        # Get the bounding box of the text
        bbox = self.get_text_bbox(text_id)
        text_x1, text_y1, text_x2, text_y2 = bbox
        bbox_h = text_y2 - text_y1
        bbox_w = text_x2 - text_x1

        # Calculate the center of the text
        text_center_x = (text_x1 + text_x2) / 2
        text_center_y = (text_y1 + text_y2) / 2

        # Determine the text and its position based on edge_pos
        if self.edge_pos == 'L':
            # Move text to the left of the location
            new_text_x = x - LeadNameObj.Offset# - bbox_w
            new_text_y = y
            anchor = 'e'  # Right align
            angle = 0
        elif self.edge_pos == 'R':
            # Move text to the right of the location
            new_text_x = x + LeadNameObj.Offset
            new_text_y = y
            anchor = 'w'  # Left align
            angle = 0
        elif self.edge_pos == 'U':
            # Move text above the location
            new_text_x = x 
            new_text_y = y - LeadNameObj.Offset
            anchor = 'w'  # Bottom align
            angle = 90
        elif self.edge_pos == 'D':
            # Move text below the location
            new_text_x = x
            new_text_y = y + LeadNameObj.Offset
            anchor = 'e'  # Top align
            angle = 90
            
        # Update the text position
        self.canvas.coords(text_id, new_text_x, new_text_y)
        self.canvas.itemconfig(text_id, anchor=anchor)
        
        # Rotate the text if necessary
        if angle != 0:
            self.canvas.itemconfig(text_id, angle=angle)

        bbox = self.get_text_bbox(text_id)
        rect_x1, rect_y1, rect_x2, rect_y2 = bbox
        
        
        if self.edge_pos == 'L':
            rect_x1 = rect_x2 - LeadNameObj.TextWidth
        elif self.edge_pos == 'R':
            rect_x2 = rect_x1 + LeadNameObj.TextWidth
        elif self.edge_pos == 'U':
           rect_y1 = rect_y2 - LeadNameObj.TextWidth
        elif self.edge_pos == 'D':
           rect_y2 = rect_y1 + LeadNameObj.TextWidth
            

        # Draw the background rectangle for the text
        if self.background_color != '':
            self.canvas.create_rectangle(rect_x1, rect_y1, rect_x2, rect_y2, fill=self.background_color, outline=self.background_color)
        
        self.canvas.tag_raise(text_id)


if __name__ == "__main__":
    # Create main window
    root = tk.Tk()
    root.title("LeadNameObj Example")

    # Create a canvas
    canvas = tk.Canvas(root, width=800, height=600)
    canvas.pack()

    # Define locations and edges for each LeadNameObj
    locations_edges = [
        ((300, 300), 'L'),  # Left
        ((300, 300), 'R'), # Right
        ((300, 300), 'U'), # Up
        ((300, 300), 'D')  # Down
    ]
    radius = 2
    canvas.create_oval(
                    300 - radius, 300 - radius,
                    300 + radius, 300 + radius,
                    fill='#f01c1c', outline='#f01c1c'
                )

    # Create LeadNameObj instances at specified locations and edges
    lead_names = []
    for loc, edge in locations_edges:
        lead_name = LeadNameObj(canvas, edge_pos=edge, location=loc)
        lead_names.append(lead_name)

    # Show all LeadNameObj instances
 #   for lead_name in lead_names:
 #       lead_name.show()

    # Run main loop
    root.mainloop()



